package com.team10.hotel.controller;

import com.team10.hotel.domain.Order;
import com.team10.hotel.domain.Room;
import com.team10.hotel.service.OrderService;
import com.team10.hotel.service.RoomService;
import com.team10.hotel.utils.OrderIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private RoomService roomService;

    @RequestMapping("/add")
    public String bookHotel(Order order, String stringInTime, RedirectAttributes redirectAttributes){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try {
            date=simpleDateFormat.parse(stringInTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        order.setInTime(date);
        String orderId= OrderIdGenerator.getOneOrderId();
        order.setOrderId(orderId);
        orderService.addOrderAndAddHot(order);
        redirectAttributes.addAttribute("orderId",orderId);
        return "redirect:/index/toPay";
    }

    @RequestMapping("/pay")
    public String payHotel(Order order,String stringInTime){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try {
            date=simpleDateFormat.parse(stringInTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        order.setInTime(date);
        orderService.saveOrUpdate(order);
        return "redirect:/frontOrders/selectAll";
    }
}
